package me.pplay.playtalk.util;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.activity.PlayTalkActivity;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.net.SkyidOperation;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpAuth;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.TpChatRoom;
import me.pplay.playtalk.protocol.bean.AuthServeripData;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;

public class PlayTalkUtil {
	private static TpAuth tpAuth;
	private static TpChat tpChat;
	private static TpChatRoom tpChatRoom;
	private static DataCallBack callback;
	private static Context context;
	private static String username = "";
	private static String password = "";
	public static final int PLAYTALK_LOGIN_OK = 2000;
	public static final int PLAYTALK_LOGIN_ERR = 2001;
	public static final int PLAYTALK_LOGIN_TIMEOUT = 2002;
	public static final int PLAYTALK_LOGIN_WAITING = 2003;
	public static boolean isLogin = false;
	public static void login(ApplicationTrans context, int skyid, DataCallBack cb) {
		isLogin = false;
		tpChat = context.getTpChat();
		tpAuth = context.getTpAuth();
		tpChatRoom = context.getTpChatRoom();
		callback = cb;
		username = "";
		password = "";
		PlayTalkUtil.context = (Context)context;
		tpChatRoom.tp_chatroom_stop();
		tpChat.tp_chat_stop();
		tpAuth.tp_auth_signin_implicit(skyid, new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				LogManager.LogShow("PlayTalkUtil", "********tp_auth_signin_implicit skyid" + result);
				if (TpAuth.AUTH_SIGIN_IMPLICIT_OK == result) {
					AuthServeripData data = (AuthServeripData) obj;
					String ip = new String(data.getIp());
					String[] iparray;
					iparray = ip.split(" ");
					Globals.token = new String(data.getToken());
					Globals.skyid = data.getSkyid();
					Globals.ChatIpString = (iparray[0]);
					Globals.FileIpString = (iparray[1]);
					Globals.ChatRoomIpString = (iparray[2]);
					checkChatAuth();
					LogManager.LogShow("PlayTalkUtil", "********checkChatAuth skyid" + Globals.skyid);
				} else if (TpAuth.AUTH_SIGIN_IMPLICIT_ERR == result||TpAuth.AUTH_GETSKYID_ERR == result) {
					callback.Callback(PLAYTALK_LOGIN_ERR, resultdata, obj);
					cleardata();
				} else if (TpAuth.AUTH_SIGIN_IMPLICIT_TIMEOUT == result||TpAuth.AUTH_TIMEROUT_ERR== result) {
					LogManager.LogShow("PlayTalkUtil", "AUTH_SIGIN_IMPLICIT_TIMEOUT");
					callback.Callback(PLAYTALK_LOGIN_TIMEOUT, resultdata, obj);
					cleardata();
				} 
			}
		});
	}

	public static void login(ApplicationTrans context, String username, String password, DataCallBack cb) {
		if("".equals(username)){
			login(context, Globals.skyid, cb);
			return;
		}
		isLogin = false;
		PlayTalkUtil.username = username ;
		PlayTalkUtil.password = password;
		tpChat = context.getTpChat();
		tpAuth = context.getTpAuth();
		tpChatRoom = context.getTpChatRoom();
		callback = cb;
		PlayTalkUtil.context = (Context)context;
		tpChatRoom.tp_chatroom_stop();
		tpChat.tp_chat_stop();
		tpAuth.tp_auth_signin_explicit(username, password, new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				if (TpAuth.AUTH_SIGIN_EXPLICIT_OK == result) {
					AuthServeripData data = (AuthServeripData) obj;
					String ip = new String(data.getIp());
					String[] iparray;
					iparray = ip.split(" ");
					Globals.token = new String(data.getToken());
					Globals.skyid = data.getSkyid();
					Globals.ChatIpString = (iparray[0]);
					Globals.FileIpString = (iparray[1]);
					Globals.ChatRoomIpString = (iparray[2]);
					
					checkChatAuth();
					LogManager.LogShow("PlayTalkUtil", "********tp_auth_signin_explicit");
				} else if (TpAuth.AUTH_SIGIN_EXPLICIT_ERR == result) {
					callback.Callback(PLAYTALK_LOGIN_ERR, resultdata, obj);
					cleardata();
				} else if (TpAuth.AUTH_SIGIN_EXLICIT_TIMEOUT == result) {
					LogManager.LogShow("PlayTalkUtil", "AUTH_SIGIN_IMPLICIT_TIMEOUT");
					callback.Callback(PLAYTALK_LOGIN_TIMEOUT, resultdata, obj);
					cleardata();
				} else if (TpAuth.AUTH_SIGIN_EXPLICIT_WAITING == result||TpAuth.AUTH_TIMEROUT_ERR== result) {
					LogManager.LogShow("PlayTalkUtil", "AUTH_SIGIN_EXPLICIT_WAITING");
					callback.Callback(PLAYTALK_LOGIN_WAITING, resultdata, obj);
					cleardata();
				} 
			}
		});
	}

	private static void checkChatAuth() {
		tpChat.tp_chat_auth(Globals.token, new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				Message msg = new Message();
				msg.what = result;
				msg.obj = resultdata;
				if(tpAuth !=null)//
					tpAuth.tp_auth_stop();
				LogManager.LogShow("PlayTalkUtil", "********CHAT_AUTH_OK  Callback result= " + result);
				if (TpChat.CHAT_AUTH_OK == result) {
					LogManager.LogShow("PlayTalkUtil", "checkChatAuth AUTH_SIGUP_EXPLICIT_OK");
					checkChatRoomAuth();
				} else if (TpChat.CHAT_AUTH_ERR == result) {
					callback.Callback(PLAYTALK_LOGIN_ERR, resultdata, obj);
					cleardata();
					LogManager.LogShow("PlayTalkUtil", "checkChatAuth AUTH_SIGIN_EXPLICIT_ERR");
				} else if (TpChat.CHAT_AUTH_TIMEOUT == result) {
					callback.Callback(PLAYTALK_LOGIN_TIMEOUT, resultdata, obj);
					cleardata();
					LogManager.LogShow("PlayTalkUtil", "checkChatAuth AUTH_SIGUP_EXLICIT_TIMEOUT");
				}

			}
		});
	}

	private static void cleardata() {
		tpChat = null;
		tpChatRoom = null;
		tpAuth = null;
		callback = null;
		context = null;
		username = password = "";
	}
	private static void getInfo() 
	{
		
		tpChat.tp_chat_get_info(Globals.skyid, new DataCallBack() {
			
			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				LogManager.LogShow("PlayTalkUtil", " ********getInfo result= " + result);
				switch (result) {
				case TpChat.CHAT_GETINFO_TIMEOUT :
					callback.Callback(PLAYTALK_LOGIN_TIMEOUT, resultdata, obj);
					cleardata();
					break;
				case TpChat.CHAT_GETINFO_ERR:
					callback.Callback(PLAYTALK_LOGIN_ERR, resultdata, obj);
					cleardata();
					break;
				case TpChat.CHAT_GETINFO_OK:
					isLogin = true;
					TPBuddyinfo myInfo = (TPBuddyinfo)obj;
					Globals.myInfo = myInfo;
					myInfo.setSkyid(Globals.skyid);
					SkyidOperation.saveSkyid(PlayTalkUtil.context);
					SkyidOperation.savePassword(PlayTalkUtil.context, PlayTalkUtil.password);
					SkyidOperation.saveUserName(PlayTalkUtil.context, PlayTalkUtil.username);
					SkyidOperation.saveTpBuddyInfo(myInfo, PlayTalkUtil.context);
					callback.Callback(PLAYTALK_LOGIN_OK, resultdata, obj);
					cleardata();
					break;
				}
				
			}
		});
	}
	private static void checkChatRoomAuth() {
		tpChatRoom.tp_chatroom_auth(Globals.token, new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				LogManager.LogShow("PlayTalkUtil", "CHATROOM_AUTH_OK  Callback result= " + result);
				if (TpChatRoom.CHATROOM_AUTH_OK == result) {
					getInfo() ;
					LogManager.LogShow("PlayTalkUtil", "checkChatroomAuth AUTH_SIGUP_EXPLICIT_OK");
				} else if (TpChatRoom.CHATROOM_AUTH_ERR == result) {
					callback.Callback(PLAYTALK_LOGIN_ERR, resultdata, obj);
					cleardata();
					LogManager.LogShow("PlayTalkUtil", "checkChatroomAuth AUTH_SIGIN_EXPLICIT_ERR");
				} else if (TpChatRoom.CHATROOM_AUTH_TIMEOUT == result) {
					callback.Callback(PLAYTALK_LOGIN_TIMEOUT, resultdata, obj);
					cleardata();
					LogManager.LogShow("PlayTalkUtil", "checkChatroomAuth AUTH_SIGUP_EXLICIT_TIMEOUT");
				}
				
			}
		});
	}
	
	public static final int TLV_TYPE_LEN = (4);
	public static final int TLV_LEN_LEN = (4);
	public static final int TLV_HEADER_LEN = (TLV_TYPE_LEN + TLV_LEN_LEN);
	public static int WriteValue(byte[] buf, int pos, byte data) {
		buf[pos] = data;
		return 1;
	}

	public static int WriteValue(byte[] buf, int pos, short data) {
		buf[pos] = (byte) ((byte) (data >> 8) & 0xff);
		buf[pos + 1] = (byte) ((byte) (data) & 0xff);
		return 2;
	}

	public static int WriteValue(byte[] buf, int pos, int data) {
		buf[pos + 0] = (byte) ((byte) (data >> 24) & 0xff);
		buf[pos + 1] = (byte) ((byte) (data >> 16) & 0xff);
		buf[pos + 2] = (byte) ((byte) (data >> 8) & 0xff);
		buf[pos + 3] = (byte) ((byte) (data) & 0xff);
		return 4;
	}

	public static int WriteValue(byte[] buf, int pos, byte[] data) {
		System.arraycopy(data, 0, buf, pos, data.length);
		return data.length;
	}

	public static short ReadWordValue(byte[] buf, int pos) {
		return (short) (((buf[0 + pos] << 8) & 0xff00) + ((buf[1 + pos]) & 0xff));
	}

	public static int ReadDWordValue(byte[] buf, int pos) {
		return ((buf[0 + pos] << 24) & 0xff000000)
				+ ((buf[1 + pos] << 16) & 0x00ff0000)
				+ ((buf[2 + pos] << 8) & 0x0000ff00)
				+ ((buf[3 + pos]) & 0x000000ff);
	}

	public static byte ReadByteValue(byte[] buf, int pos) {
		return buf[0 + pos];
	}

	public static int TLV_Encode(byte[] p, int pos, int code, int value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
//		if (0 == value) {
//			pos += WriteValue(p, pos, 0);
//			return TLV_HEADER_LEN;
//		}
		pos += WriteValue(p, pos, (int) 4);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN + 4);
	}

	public static int TLV_Encode(byte[] p, int pos, int code, short value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
//		if (0 == value) {
//			pos += WriteValue(p, pos, 0);
//			return TLV_HEADER_LEN;
//		}
		pos += WriteValue(p, pos, (int) 2);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN + 2);
	}

	public static int TLV_Encode(byte[] p, int pos, int code, byte value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
//		if (0 == value) {
//			pos += WriteValue(p, pos, 0);
//			return TLV_HEADER_LEN;
//		}
		pos += WriteValue(p, pos, (int) 1);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN + 1);
	}

	public static int TLV_Encode(byte[] p, int pos, int code, byte[] value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
		if (null == value) {
			pos += WriteValue(p, pos, 0);
			return TLV_HEADER_LEN;
		}
		pos += WriteValue(p, pos, (int) value.length);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN + value.length);
	}

	/////.....t2bytes l 2bytes
	public static int TLV_Encode(byte[] p, int pos, short code, int value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
		pos += WriteValue(p, pos, (short) 4);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN>>1) + (4);
	}

	public static int TLV_Encode(byte[] p, int pos, short code, short value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
		pos += WriteValue(p, pos, (short) 2);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN>>1) + (2);
	}

	public static int TLV_Encode(byte[] p, int pos, short code, byte value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
		pos += WriteValue(p, pos, (short) 1);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN>>1) + (1);
	}

	public static int TLV_Encode(byte[] p, int pos, short code, byte[] value) {
		if (code == 0 || p == null) {
			return 0;
		}
		pos += WriteValue(p, pos, code);
		if (null == value) {
			pos += WriteValue(p, pos, (short)0);
			return (TLV_HEADER_LEN>>1);
		}
		pos += WriteValue(p, pos, (short) value.length);
		pos += WriteValue(p, pos, value);
		return (TLV_HEADER_LEN>>1) + (value.length);
	}
}
